package com.songlanyun.modules.memberbill.controller.admin;

import java.util.List;
import java.util.Map;

import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.modules.memberbill.entity.GiveBalanceDTO;
import com.songlanyun.modules.order.service.CreateOrderService;
import com.songlanyun.modules.order.service.OrderService;
import com.songlanyun.modules.shop.service.ShopService;
import com.songlanyun.modules.user.controller.AbstractController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.songlanyun.modules.memberbill.entity.SaleRefillCardBillEntity;
import com.songlanyun.modules.memberbill.service.SaleRefillCardBillService;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;

import javax.annotation.Resource;


/**
 * 会员账单
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2020-11-16 14:58:05
 */
@RestController
@RequestMapping("admin/bill/refillCard")
public class SaleRefillCardBillAdminController extends AbstractController {
    @Autowired
    private SaleRefillCardBillService saleRefillCardBillService;

    @Resource
    private ShopService shopService;

    @Resource
    private CreateOrderService createOrderService;


    /**
     * 平台查询所有充值账单
     */
    @GetMapping("/list")
//    @RequiresPermissions("bill:saleRefillCardBill:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = saleRefillCardBillService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 商家查询自己会员的充值账单
     */
    @GetMapping("/myRefillBill")
//    @RequiresPermissions("bill:saleRefillCardBill:list")
    public R myList(@RequestParam Map<String, Object> params) {

        Long uid = shopService.getUserIdByAdminId(getUserId(),false);//会员id
        params.put("user_id", uid);

        PageUtils page = saleRefillCardBillService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        SaleRefillCardBillEntity saleRefillCardBill = saleRefillCardBillService.getById(id);

        return R.ok().put("data", saleRefillCardBill);
    }

    /**
     * 查询会员的所有钱包明细
     */
    @GetMapping("/get-user-bill/{userId}")
    public R getUserBill(@PathVariable("userId") Long userId,@RequestParam Map<String, Object> params) {
//        List<SaleRefillCardBillEntity> list = saleRefillCardBillService.getUserBill(userId);
        PageUtils page = saleRefillCardBillService.getUserBillPage(userId,params);

        return R.ok().put("page", page);
    }

    /**
     * 调整会员余额
     */
    @PostMapping("/adjustBalance")
    public R adjustBalance(@RequestBody GiveBalanceDTO dto) {
        saleRefillCardBillService.adjustBalance(dto);
        return R.ok();
    }

    /**
     * 调整会员余额
     */
    @PostMapping("/adjustIntegral")
    public R adjustIntegral(@RequestBody GiveBalanceDTO dto) {
        ValidatorUtils.validateEntity(dto);
        saleRefillCardBillService.adjustIntegral(dto);
        return R.ok();
    }
    /**
     * 用户线下购买商品
     */
    @PostMapping("/buyGoods")
    public R buyGoods(@RequestBody GiveBalanceDTO dto) {
        // 后台创建订单
        createOrderService.backStageManagement(dto);
        return R.ok();
    }

}
